<!--
 * @Descripttion: (销售订单/V_PROD)
 * @Author: (admin)
 * @Date: (2025-06-19)
-->
<template>
  <div>
   <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
    <el-form-item label="状态" prop="ckstatus">
            <el-select v-model="queryParams.ckstatus"  placeholder="状态" clearable style="width: 140px" multiple>
              <el-option label="全部"  value="" />
              <el-option v-for="dict in options.statusOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
            </el-select>
          </el-form-item>
       <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      > 
      <el-table-column prop="prod" label="产品代码" :show-overflow-tooltip="true"   width="100"  v-if="columns.showColumn('prod')"/>
      <el-table-column prop="spec" label="规格 " align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('spec')"/>
      <el-table-column prop="code" label="机器号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('code')"/>
      <el-table-column prop="vpno" label="批号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('vpno')"/>
      <el-table-column prop="remark" label="具体事宜" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('remark')"/>
      <el-table-column prop="ddbh" label="销售单号" align="center" v-if="columns.showColumn('ddbh')"/>
      <el-table-column prop="hzorder" label="计划令" align="center" v-if="columns.showColumn('hzorder')"/>
      <el-table-column prop="outTime" label="出货时间" align="center" v-if="columns.showColumn('outTime')"/>
      <el-table-column prop="ckstatus" label="在库状态" align="center" v-if="columns.showColumn('ckstatus')"/>
        <el-table-column prop="cqty" label="现有库存" align="center" v-if="columns.showColumn('cqty')"/>
       <!-- <el-table-column label="操作" width="260">
        <template #default="scope">
          <el-button type="primary" size="small" icon="Lightning" title="详情" @click="handlePreview(scope.row)"></el-button>
            </template>
      </el-table-column> -->
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

 
  </div>
</template>

<script setup name="SaleVPROD">
import { listSalePROD,  getSalePROD 
 } 
  from '@/api/buss/salevprod.js' 
  import { API_QueryAI } from '@/api/common.js' 
const { proxy } = getCurrentInstance()
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  PROD: undefined,
  ckstatus: ['在库','分配'], // 默认选中"在库"状态
  sort: 'PROD',
  sortType: 'asc'
})
const  options= { 
    statusOptions: [
      { dictLabel: '在库', dictValue: '在库', listClass: 'success'  },
      { dictLabel: '出库', dictValue: '出库', listClass: 'danger' },
       { dictLabel: '分配', dictValue: '分配', listClass: 'danger'  }
    ]
  }

const aiDialog = ref()
const ids = ref([])
const loading = ref(false)
const route = useRoute()
const showSearch = ref(true)

watch(
  () => route.query.prod,
  (val) => {
    if (val) { 
      getListInit(val,status)
    }
  },
  {
    immediate: true
  }
)

const columns = ref([
  { visible: true, align: 'center', type: '', prop: 'xh', label: '序号'   },
  { visible: true, align: 'center', type: '', prop: 'orderDate', label: '订单日期'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'orderNo', label: '订单号'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'cust', label: '客户简称'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'model', label: '机型'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'remark', label: '具体事宜'  ,showOverflowTooltip: true  },
  { visible: true, align: 'center', type: '', prop: 'actQuantity', label: '实际数量'   },
  { visible: true, align: 'center', type: '', prop: 'shippedQuantity', label: '已出货数'   },
  { visible: false, align: 'center', type: '', prop: 'unshippedQuantity', label: '未出货数'   },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]
function   processSelectedStatus() {
  if (queryParams.ckstatus && queryParams.ckstatus.length > 0) {
    console.log('用户选择了:', queryParams.ckstatus.join(', '))
    // 这里可以添加您的处理逻辑
    return queryParams.ckstatus.join(',');
  } else {
    return ''
  }
}

function getListInit(prod,status){
  loading.value = true  
  queryParams.PROD = prod;
  var parp={
  pageNum: 1,
  pageSize: 10,
  PROD: prod,
    ckstatus: ['在库', '分配'],
  ckstatusStr: "在库,分配",
  sort: 'PROD',
  sortType: 'asc'
  }  
  listSalePROD(parp).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}
function getList(){
  loading.value = true   
  
  queryParams.ckstatusStr = processSelectedStatus();
//  console.log(dateRange) 
  listSalePROD(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}
// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}
 
/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  var data = {
    title: 'GbSalesOrder',
    Prompt: row.model,
  };
 API_QueryAI(data).then((res) => {
    const data = JSON.stringify(res.data.table1[0] || '无') ;
     const prom = JSON.stringify(res.data.table2[0].prompt||'无') ;
   //console.log(data)

    aiDialog.value?.openDialog()
  aiDialog.value?.clearMessages();
  // 准备提示词
  const prompt =  data +prom
// 请从以下几个方面进行分析：
// 1. 库存水平评估
// 2. 可能的补货建议
// 3. 潜在问题分析
// 4. 优化建议
  // 添加用户消息
  aiDialog.value?.addMessage({
    role: 'user',
    content:  prompt  
  })

  // 添加空的AI消息用于流式响应
  aiDialog.value?.addMessage({
    role: 'assistant',
    content: '',
    isStreaming: true
  })

  // 调用DeepSeek API
  aiDialog.value?.queryAI(prompt)
  })
  
}
 
  
</script>